Repository navigation
feat(context): prototype context-scoped attributes - #8947
Conversation
Pull request dashboard statusMerged · refreshed 2026-11-12 21:17 UTC Status above doesn't look right?
|
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #8947 +/- ##
============================================
+ Coverage 90.37% 90.41% +0.03%
- Complexity 11018 11069 +51
============================================
Files 1021 1023 +2
Lines 30124 30254 +130
Branches 3933 3952 +19
============================================
+ Hits 27225 27353 +128
+ Misses 1941 1933 -8
- Partials 958 968 +10 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
403a57c to
7d8f465
Compare
3b2ce44 to
e7872d7
Compare
e7872d7 to
657d862
Compare
Implements prototype for Context-scoped attributes per OTEP open-telemetry#4931 and Issue open-telemetry#8532: - Add ContextAttributes API in :api:incubator for setting/getting/extracting attributes on Context - Support opt-in context-scoped attributes configuration across SdkTracerProvider, SdkLoggerProvider, and SdkMeterProvider - Inherit context attributes in SdkSpanBuilder, SdkLogRecordBuilder, and synchronous metric storage (Delta & Cumulative) - Expose configuration via declarative-config (tracer_provider, logger_provider, meter_provider) - Add comprehensive unit, incubating, and multi-signal integration tests
657d862 to
ef1468f
Compare
|
@jack-berg Please take a look . Thank you. |
| if (context == null) { | ||
| return Attributes.empty(); | ||
| } | ||
| Attributes attributes = context.get(CONTEXT_ATTRIBUTES_KEY); |
There was a problem hiding this comment.
I see usefulness in this feature but I find it hard to understand how to decide with instrumentations should include the attributes.
How to assert or qualify the carnality of the attribute?
Which instrumentations should skip an attribute?
Should we have a handler to decide which instrumentations opt to an attribute?
Are there attributes that make no sense across an entire context lifespan?
There was a problem hiding this comment.
Instrumentations are advised against including attributes (see javadoc at top).
Think about the core use cases: tenant id / user id. An application would add these to context early in request lifecycle via interceptor or filter. They would also configure the SDK to include the attributes as needed based on signal, scope, and and attribute key.
The OTEP left a lot of questions unanswered with the intent of discovering more via prototyping. If you have general feedback (i.e. not related to the java implementation), please add it to open-telemetry/opentelemetry-specification#5170 so all languages can benefit.
jack-berg
left a comment
There was a problem hiding this comment.
Pretty good stuff - close to what I expected. Left a few comments but nothing should be too tricky.
This comment has been minimized.
This comment has been minimized.
…oped attributes prototype (open-telemetry#8532) - Trace: Isolate parent context and ambient Baggage when setNoParent() is configured by passing Context.root() to samplers/processors while resolving context attributes from current context. - Collisions: Add amortized O(1) AttributesMap.contains(AttributeKey<?>) to enforce name-based attribute identity and ensure explicit attributes take precedence over context attributes regardless of type. - Logs: Eliminate AttributesMap.copyOf defensive allocation by mutating builder attributes in-place during emit(). - Filtering: Upgrade trace, log, and metric providers from a boolean flag to BiPredicate<InstrumentationScopeInfo, AttributeKey<?>> contextScopedAttributesFilter; curry predicate in SdkMeter for synchronous metric storage; preserve context-oblivious bound instruments. - API: Add ContextAttributes.set() for replacement and clearing, retain add() for additive merging, enforce strict requireNonNull validation, and record maintainer deprecation TODO. - Config: Revert declarative-config prototype additions to match upstream main.
12e5e76 to
a9bcfb9
Compare
| if (context == null) { | ||
| return Attributes.empty(); | ||
| } | ||
| Attributes attributes = context.get(CONTEXT_ATTRIBUTES_KEY); |
There was a problem hiding this comment.
Instrumentations are advised against including attributes (see javadoc at top).
Think about the core use cases: tenant id / user id. An application would add these to context early in request lifecycle via interceptor or filter. They would also configure the SDK to include the attributes as needed based on signal, scope, and and attribute key.
The OTEP left a lot of questions unanswered with the intent of discovering more via prototyping. If you have general feedback (i.e. not related to the java implementation), please add it to open-telemetry/opentelemetry-specification#5170 so all languages can benefit.
…tributes prototype (open-telemetry#8532) - API: Gate deprecation TODO on spec PR 4931, streamline Javadoc to focus on opt-in SDK stamping and metric cardinality risks, and gracefully degrade on null params via ApiUsageLogger. - AttributesMap: Rename contains(AttributeKey) to containsKey(String) to reflect name-only lookup semantics. - Providers: Remove deprecated setContextScopedAttributesEnabled(boolean) across Trace, Log, and Metric providers and utils in favor of the canonical filter predicate. - Logs: Extract populateContextAttributes helper with fast-exit on empty context attributes, and remove redundant compiler warning suppressions. - Metrics: Guard incubating reflection utilities with INCUBATOR_AVAILABLE across all signals, instantiate AttributesBuilder lazily only on first matching attribute, return attributes unchanged on miss, and clarify bound instruments Javadoc with review TODO. - Tests: De-duplicate log test fixtures via @beforeeach, update all integration and unit test setups to use filter predicates, and verify graceful null degradation.
2608242 to
c845277
Compare
2c79d44 to
802432d
Compare
…tributes prototype (open-telemetry#8532) - api: remove add(Context, Attributes) and strip @nullable parameter annotations - api: update Javadoc on metric cardinality and bound instruments - api: relocate integration test to ContextAttributesUsageTest and document in README - trace: use NO_PARENT sentinel Context in SdkSpanBuilder to eliminate boolean field - metrics: pre-curry contextScopedAttributesFilter in SdkMeter constructor - metrics: relocate INCUBATOR_AVAILABLE to top of DefaultSynchronousMetricStorage - logs: remove redundant this. qualifier on attributes in SdkLogRecordBuilder - tests: modernize and streamline assertions across signals using satisfiesExactly and Mockito
Overview
Prototypes Context-scoped attributes (OTEP #4931, #8532), enabling Traces, Logs, and synchronous Metrics to automatically inherit attributes from the active
Context.See https://github.com/open-telemetry/opentelemetry-java/tree/main/api/incubator#context-scoped-attributes for a usage demonstration.
Specification Contracts & Architectural Invariants
api:incubator):ContextAttributes.set(Context, Attributes): Sets or replaces context-scoped attributes on a context.ContextAttributes.get(Context): Reads context-scoped attributes (returnsAttributes.empty()if absent).ApiUsageLoggerfail-soft guards.setNoParent()(sdk:trace):Sampler.shouldSample(), ensuring sampling, span creation, andSpanProcessor.onStart()observe the merged set.setNoParent()isolates trace parentage and baggage to root while preserving ambient context attributes, using a staticNO_PARENTcontext sentinel to eliminate builder heap overhead.sdk:metrics):BoundStorageHandle) record strictly to their pre-bound timeseries with zero runtime context evaluation or heap allocations.Viewfiltering.SpanLimits,LogLimits) and MetricViewfilters apply post-merge.Sdk*ProviderUtil.setContextScopedAttributesFilter(builder, BiPredicate<InstrumentationScopeInfo, AttributeKey<?>>).Signal Implementation Summary
ContextAttributesset()and readerget()backed by a privateContextKey.SdkSpanBuilder.startSpan()Sampler.shouldSample(); preserves ambient attributes onsetNoParent().SdkLogRecordBuilder.emit()LogLimitsand tracks dropped attributes.DefaultSynchronousMetricStorageSdk*ProviderUtilBiPredicate<InstrumentationScopeInfo, AttributeKey<?>>.Documentation & Verification
ContextAttributesUsageTest; documented inapi/incubator/README.md.sdk:trace:ContextScopedAttributesSpanTest(sampler inspection, trace/baggage detachment, attribute inheritance).sdk:metrics:ContextScopedAttributesMetricTest(unbound dynamic inheritance, bound isolation across aggregation temporalities).sdk:logs:ContextScopedAttributesLogTest(attribute inheritance, collision precedence,LogLimitsaccounting).spotlessCheck,checkstyle,animalsniffer, andjApiCmp.